/**
 * 
 */
package com.brother.goallife.dao.impl;

import java.security.Principal;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.brother.goallife.HibernateDaoTemplate;
import com.brother.goallife.dao.AccountDAO;
import com.brother.goallife.dto.Account;
import com.brother.goallife.exception.FatalException;
import com.brother.goallife.utils.GoallifeUtils;

/**
 * @author anhtuan_truong
 * 
 */
public class AccountDAOImpl extends HibernateDaoTemplate<Account> implements
		AccountDAO {

	public AccountDAOImpl(Class classType) {
		super(classType);
	}

	public Account findByUsername(String username) throws FatalException {
		DetachedCriteria cri = DetachedCriteria.forClass(Account.class);
		cri.add(Restrictions.eq("username", username));
		return findFirstByCriteria(cri);
	}

	public Principal login(String username, String password)
			throws FatalException {
		Account user = findByUsername(username);
		if (user.getPassword() != null && user.getPassword().equals(password)
				&& GoallifeUtils.isActive(user))
			return user;

		return null;
	}
	
	@Override
	public void updateAccount(Account account) throws FatalException {
		super.update(account);
	}

	

}
